package com.tabview.difficultpoint;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 作者：zhouXJ on 2016/3/30 14:20
 */
public class AttendanceFragment extends BaseUiFragment {

    private ListView attendance_list;

    // 标志位，标志已经初始化完成。
    private boolean isPrepared = false;

    private ListView class_course_difficult_points_list;
    private LinearLayout class_course_difficult_points_ll;
    private RelativeLayout class_course_difficult_points_empty_rl;
    private List<CourseDifficultPointContent> contentList;
    private AttendanceAdapter attendanceAdapter;
    private HashMap<String, String> params;

    private String orderinfooaid;
    private String agencyid;
    private String studentid;

    public void setStudentid(String studentid) {
        this.studentid = studentid;
    }

    public void setAgencyid(String agencyid) {
        this.agencyid = agencyid;
    }

    public void setOrderinfooaid(String orderinfooaid) {
        this.orderinfooaid = orderinfooaid;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_attendance, container, false);

        isPrepared = true;
        lazyLoad();
        return view;
    }

    @Override
    protected void initData() {
        attendance_list = (ListView) findViewById(R.id.attendance_list);
        class_course_difficult_points_list = (ListView) findViewById(R.id.class_course_difficult_points_list);
        class_course_difficult_points_ll = (LinearLayout) findViewById(R.id.class_course_difficult_points_ll);
        class_course_difficult_points_empty_rl = (RelativeLayout) findViewById(R.id.class_course_difficult_points_empty_rl);

        attendanceAdapter = new AttendanceAdapter(getActivity());
        attendance_list.setAdapter(attendanceAdapter);

    }

    private Handler pointHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            XApplication.hideLoadingDialog();
            if (CourseDifficultPointsTitleTask.SUCCESS == msg.what) {
                CourseDifficultPointContent[] svoArr = (CourseDifficultPointContent[]) msg.getData().getSerializable("schedules");
                if (null == svoArr || svoArr.length <= 0) {
                    class_course_difficult_points_empty_rl.setVisibility(View.VISIBLE);
                    return;
                }

                contentList = new ArrayList<CourseDifficultPointContent>();
                contentList.clear();
                contentList.addAll(Arrays.asList(svoArr));
                attendanceAdapter.clear();
                attendanceAdapter.addAll(contentList);


            } else if (CourseDifficultPointsTitleTask.ERROR == msg.what) {
                Toast.makeText(getActivity(), "请求失败，请稍后重试", Toast.LENGTH_SHORT).show();
            }
        }
    };


    @Override
    protected void initListener() {

    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }

        params = Maps.newHashMap();
        params.put("studentid", studentid);
        params.put("agencyid", agencyid);
        params.put("orderinfooaid", orderinfooaid);

        XApplication.showLodingDialog("正在加载", getActivity());
        CourseDifficultPointsTask pointsTask = new CourseDifficultPointsTask(getActivity(), pointHandler);
        pointsTask.execute(params);
    }
}
